package view;

import java.util.Collection;

import javax.swing.table.AbstractTableModel;

import model.Registration;
import persistency.controller.RegistrationController;

public class ScheduleModel extends AbstractTableModel {
	/* Required for the serializable interface */
	private static final long serialVersionUID = 1L;
	/* List of column names */
	private static final String[] columnNames = { "Student", "Course", "Classroom", "Time", "Teacher","RecieveEmail" };
	/* List of offered courses */
	Collection<Registration> registrations;
	
	/** Constructs a new schedule model */
	public ScheduleModel(Collection<Registration> registrations) {
		this.registrations = registrations;
	}
	
	/** Returns the row count of the list */
	@Override
	public int getRowCount() {
		return this.registrations.size();
	}
	
	/** Returns the column count of the list */
	@Override
	public int getColumnCount() {
		return 6;
	}
	
	/** Returns the column name */
	@Override
	public String getColumnName(int column) {
		return columnNames[column];
	}
	
	/** Returns the appropriate value for given row and column */
	@Override
	public Object getValueAt(int row, int column) {
		try{
		Registration[] regArray = new Registration[this.getRowCount()];
		regArray = this.registrations.toArray(regArray);
		Registration registration = regArray[row];

		switch (column) {
			case 0:
				return registration.getStudent();
			case 1:
				return registration.getOfferedCourse().getCourse();
			case 2:
				return registration.getOfferedCourse().getClassroom();
			case 3:
				return registration.getOfferedCourse().getTimeBlock();
			case 4:
				return registration.getOfferedCourse().getTeacher();
			case 5:
				return registration.isReceiveCourseEmail();
			case 6:
				return registration.getId();
			default:
				return "";
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return (columnIndex == 5);
	}
	
	public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
	
	public void setValueAt(Object value, int row, int col) {
		Object id = getValueAt(row, 6);
		RegistrationController.instance().getRegistration(id.toString()).setReceiveCourseEmail(Boolean.parseBoolean(value.toString()));
        fireTableCellUpdated(row, col);
    }
	
	
	
	
}